<?php
/*
	申请成为代理商
	如果已申请，但尚未审核通过，则页面显示请耐心等待管理员审核！
	如果已申请，且审核通过，则显示审核通过，并提供按钮前往代理中心
	如果审核不通过，则显示审核不通过原因
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
cfc('core')->checklogin();
if($_RQ['op']=='main'){
	set_html('title','申请成为'.$this->shifutitle);
	$member=$this->pdo_get('member',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
	//判断是否满足申请分销商条件
	$can_apply=true;
	$return['can_apply']=$can_apply;	
	$return['member']=$member;
	$return['post']=model('core/member')->detail($_SESSION['uid'],array('nickname','avatar','realname','mobile','wechat'));
	$return['post']['diyfields']=$this->setting['diyformfields']?:array();
	$groups=pdo_getall('jy_wxshifu_area',array('uniacid'=>$_SESSION['uniacid']),'','id');
	$return['groups'][0]=array('id'=>0,'title'=>'无服务点');
	foreach($groups as $k=>$item){
		$return['groups'][]=$item;
		if($item['id']==$return['post']['group_id']){
			$return['post']['groupindex']=$k;
		}
	}
	exi($return);
}
if($_RQ['op']=='apply'){
	$post=get_postdata();
	if($_SESSION['container']!='wxapp' && $return['pluginset']['is_smscode'] && !check_sms_code($post['smscode'])){
		//exi('手机验证码错误！','error');
	}
	if(!$post['agree']){
		exi('请先阅读协议！','error');
	}
	if(!$post['realname']){
		exi('请输入姓名！','error');
	}
	if(!$post['mobile']){
		exi('请输入联系电话！','error');
	}
	if(!$post['group_id']){
		//exi('请输入申请服务点！','error');
	}
	if($this->setting['fee_open'] && $this->setting['fee_price']>0){
		$status=3;
	}
	$data=array(
			'title'=>trim($post['realname']),
			'mobile'=>trim($post['mobile']),
			'name2'=>trim($post['name2']),
			'mobile2'=>trim($post['mobile2']),
			'guanxi2'=>trim($post['guanxi2']),
			'cardimg1'=>trim($post['cardimg1']),
			'cardimg2'=>trim($post['cardimg2']),
			'address'=>trim($post['address']),
			'wechat'=>trim($post['wechat']),
			'group_id'=>intval($post['group_id']),
			'desc'=>trim($post['desc']),
			'diyformdata'=>ijson_encode($post['diyfields']),
			'status'=>$status,
			'viptime'=>time(),
	);
	$pid=$this->model('member')->register($data);
	if($this->setting['fee_open'] && $this->setting['fee_price']>0){
			$insert=array(
					'uniacid'=>$_SESSION['uniacid'],
					'mid'=>$_SESSION['uid'],
					'pid'=>$pid,
					'money'=>$this->setting['fee_price'],
					'createtime'=>TIMESTAMP,
			);
			pdo_insert('jy_wxshifu_mfee_log',$insert);
			$id=pdo_insertid();
			$params['fee']=$this->setting['fee_price'];
			$params['title']=$this->shifutitle.'入驻';
			$params['id']=$id;
			$params['plugin']='jy_wxshifu';
			$params['back_url']='jy_wxshifu/apply/payresult/';
			$payid=cfc('pay')->create_pay_order($params);
			$this->pdo_update('mfee_log',array('pay_id'=>$payid),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
			$res=array(
					'pay_id'=>$payid,
					'order_id'=>$id,
			);
			exi($res);
	}else{
		$this->model('member')->apply_agent($pid);
	}
	exi('申请成功！请耐心等待管理员审核！','','jy_wxshifu/index/');
}
if($_RQ['op']=='payresult'){
	$payresult=$_SESSION['pay_result']; 
	if(!$payresult){
		exi('支付失败，未收到支付结果！','error','main');
	}
	if($payresult['status']!=1){
		exi('支付失败！','error','main');
	}
	$order=pdo_get('jy_wxshifu_mfee_log',array('id'=>$payresult['id'],'uniacid'=>$_SESSION['uniacid']));
	if(!$order){
		exi('支付失败！指定订单不存在！','error');
	}
	if($order['status']){
		exi('订单已支付，无需重复支付！','error');
	}
	if($order['money']!=$payresult['fee']){
		exi('支付失败！订单金额不匹配！','error');
	}
	pdo_update('jy_wxshifu_mfee_log',array('status'=>1),array('id'=>$order['id'],'uniacid'=>$_SESSION['uniacid']));
	pdo_update('jy_wxshifu_member',array('status'=>0,'isvip'=>1,'viptime +='=>365*24*3600),array('id'=>$order['pid'],'uniacid'=>$_SESSION['uniacid']));
	exi('支付成功','success','jy_wxshifu/index/');
}